package com.sdu.cloudservice.service.impl;

import com.sdu.cloudservice.dao.AdminDao;
import com.sdu.cloudservice.dao.UserDao;
import com.sdu.cloudservice.dto.Envelope;
import com.sdu.cloudservice.entity.AdminEntity;
import com.sdu.cloudservice.entity.UserEntity;
import com.sdu.cloudservice.service.AdminService;
import com.sdu.cloudservice.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * LXF,ZMX
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UserDao userdao;
    private int onePage = 20;//一页的行数
    @Autowired
    private AdminDao adminDao;
    private AdminEntity adminEntity;

    String pattern = "^[a-zA-Z0-9]{4,16}$";
    /**
     * 分页查询用户操作
     *
     * @param currentPage 默认0 检查合法性   0-maxPage
     * @param code        默认0 login-register  (默认2级login)1 username   2 registerDate  3 picDownload   4 excelDownload 5 queryTimes
     * @return list<entity>、非法page
     */
    @Override
    public Envelope getUserList(int currentPage, int code) {
        int numUser = userdao.getNumOfUser();
        int maxPage = numUser/onePage + 1;
        if(currentPage<0||currentPage>maxPage)
            return new Envelope(7,"非法页数",null);
        //分析code
        int start = (currentPage-1)*onePage;
        List<UserEntity> userList = new ArrayList<UserEntity>();
        switch (code){
            case 0:
                userList = userdao.ListUserByLastLogin(start,onePage);
                return new Envelope(userList);
            case 1:
                userList = userdao.ListUserByRegisterDate(start,onePage);
                return new Envelope(userList);
            case 2:
                userList = userdao.ListUserByUsername(start,onePage);
                return new Envelope(userList);
            case 3:
                userList = userdao.listUserByExcelDownload(start,onePage);
                return new Envelope(userList);
            case 4:
                userList = userdao.listUserByPicDownload(start,onePage);
                return new Envelope(userList);
            case 5:
                userList = userdao.listUserByQueryTimes(start,onePage);
                return new Envelope(userList);
            default:
                return new Envelope(8,"无效code",null);
        }
    }

    @Override
    public Envelope getPageNumber() {
        int numUser = userdao.getNumOfUser();
        // int maxPage = numUser/onePage + 1;
        return new Envelope(numUser);
    }

    @Override
    public Envelope login(String username, String password) {
        if(!username.matches(pattern))
            return new Envelope(5,"格式错误",null);
        adminEntity=adminDao.findAdminByUsername(username);
        if(adminEntity==null)
            return new Envelope(1,"用户名或密码错误",null);
        else{
            if(PasswordUtil.getMd5(password).equals(adminEntity.getPassword()))
                return new Envelope(adminEntity);
            else
                return new Envelope(6,"用户名或密码错误",null);
        }
    }

    @Override
    public Envelope verify_password(String username, String password) {
        adminEntity=adminDao.findAdminByUsername(username);
        if(PasswordUtil.getMd5(password).equals(adminEntity.getPassword()))
            return new Envelope(0,"密码输入正确",null);
        return new Envelope(7,"用户名与密码不匹配，请重新输入密码",null);
    }

    @Override
    public Envelope change_password(String username, String newpassword) {
        if(newpassword.length()>=6&&newpassword.length()<=20){
            String md5_new_password=PasswordUtil.getMd5(newpassword);
            adminDao.update(username,md5_new_password);
            Envelope envelope=new Envelope(adminEntity);
            envelope.setMessage("密码修改成功");
            envelope.setCode(0);
            return envelope;
        }
        else if(newpassword.length()>20)
            return new Envelope(9,"密码过长",null);
        else
            return new Envelope(10,"密码过短",null);
    }
}
